package com.example.administrator.basedemos.typeface;

import android.content.Context;
import android.graphics.Typeface;

import com.mikepenz.iconics.typeface.IIcon;
import com.mikepenz.iconics.typeface.ITypeface;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * 创建者     ZCL
 * 创建时间   2018/3/29 17:57
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 */

public class ZclFont implements ITypeface {
    private static final String   TTF_FILE = "zclfont.ttf";
    private static       Typeface typeface = null;
    private static HashMap<String, Character> mChars;

    @Override
    public IIcon getIcon(String key) {
        return Icon.valueOf(key);
    }

    @Override
    public Collection<String> getIcons() {
        Collection<String> icons = new LinkedList<String>();
        for (Icon value : Icon.values()) {
            icons.add(value.name());
        }
        return icons;
    }

    @Override
    public HashMap<String, Character> getCharacters() {
        if (mChars == null) {
            HashMap<String, Character> aChars = new HashMap<String, Character>();
            for (Icon v : Icon.values()) {
                aChars.put(v.name(), v.character);
            }
            mChars = aChars;
        }
        return mChars;
    }

    /**
     * zcl_cart('&#xe6ac');转换成unicode编码
     * 设置前缀最好和getMappingPrefix给的一致
     */
    @Override
    public String getMappingPrefix() {
        return "zcl";
    }
    //字体名字
    @Override
    public String getFontName() {
        return "zclfont";
    }
    @Override
    public String getVersion() {
        return "1.0.0";
    }
    @Override
    public int getIconCount() {
        return mChars.size();
    }
    @Override
    public String getAuthor() {
        return "zcl";
    }

    @Override
    public String getUrl() {
        return "http://WWW.zcl.com/";
    }

    @Override
    public String getDescription() {
        return "The premium icon font for Ionic Framework.";
    }

    @Override
    public String getLicense() {
        return "MIT Licensed";
    }

    //授权地址信息可以自己设置网站
    @Override
    public String getLicenseUrl() {
        return "http://WWW.zcl.com/";
    }

    @Override
    public Typeface getTypeface(Context context) {
        if (typeface == null) {
            try {
                /**
                 * Typeface.createFromAsset(getAssets(), "iconfont.ttf");
                 * 设置使用方式
                 */
                typeface = Typeface.createFromAsset(context.getAssets(),TTF_FILE);
            } catch (Exception e) {
                return null;
            }
        }
        return typeface;
    }

    public enum Icon implements IIcon {
        /**
         * zcl_cart('&#xe6ac');转换成unicode编码
         * 前缀最好和getMappingPrefix给的一致
         */
        zcl_search('\ue6ac'),
        zcl_android_bus('\uf36d'),
        zcl_cart('\ue63f');

        char character;

        Icon(char character) {
            this.character = character;
        }

        public String getFormattedName() {
            return "{" + name() + "}";
        }

        public char getCharacter() {
            return character;
        }

        public String getName() {
            return name();
        }

        // remember the typeface so we can use it later
        private static ITypeface typeface;
        /**
         * 设置成我们自己的文件ZclFont
         * @return
         */
        public ITypeface getTypeface() {
            if (typeface == null) {
                typeface = new ZclFont();
            }
            return typeface;
        }
    }
}

